<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\library\Token;
use app\common\library\Sms;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
        return $this->view->fetch();
    }

    public function qqlogin(){
        $qc = new \qqlogin\QC();
        $url = $qc->qq_login();
        $this->redirect($url);
    }

    public function pay(){
        $type = input('post.type');
        if ($type !== 'wxpay' && $type !== 'qqpay' && $type !== 'alipay') $this->error('支付类型出错！');
        $money = config("site.test_pay_money");
        $money = round($money,2);
        if (!is_numeric($money) || $money <= 0) $this->error('金额格式不正确');
        return Pay([
            'money'=>$money,
            'type'=>$type,
            'title' => '支付体验',
            'notify_url' => request()->domain().'/index/index/pay_verify',
            'return_url' => request()->domain().'/index/index/pay_verify',
            'config'=>''
        ]);
    }

    public function pay_verify(){
        $verify = Pay([],false);
        if ($verify){
            $this->redirect(request()->domain().'/index/index/test?msg=效验成功！订单已支付');
        }else{
            $this->error('支付出错！','/index/index/index');
        }
    }
    
    public function test()
    {
        if (input('msg')) $this->assignconfig('C',['msg'=>input('msg')]);
        if (config('site.test_pay').'' !== '1') $this->error('未开启测试支付','index');
        $view = 'test_pay_pc';
        $this->assignconfig('V',$view);
        return $this->view->fetch($view);
    }

    public function doc()
    {
        $url = config('site.doc_url');
        $this->redirect($url);
    }

    public function wxopenid()
    {
        $Appid=config("site.wx_public_appid");
        $AppSecret=config("site.wx_public_appsecret");
        $tools = new \wx\Openid($Appid,$AppSecret);
        $openid = $tools->GetOpenid();
        $this->assign(['openid'=>$openid]);
        return $this->fetch();
    }
}
